package com.millstein.tsinglog.controller;

import com.millstein.tsinglog.service.ArticleService;
import com.millstein.tsinglog.service.NoticeService;
import com.millstein.tsinglog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

public class BaseController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private TagService tagService;

    /**
     * 给前端页面准备公共的数据
     * @param model
     */
    protected void getCommonDataForPortal(Model model) {

        // 给页面准备热门文章的数据
        List<Map<String, Object>> hotArticleList = articleService.getHotArticle();
        // 给页面准备公告数据
        List<Map<String, Object>> noticeList = noticeService.getNoticeListForPortal();
        // 给页面准备标签信息
        Map<String, Object> tagMap = tagService.getTagListForPortal();

        if (!CollectionUtils.isEmpty(hotArticleList)) {
            model.addAttribute("hotArticleList", hotArticleList);
        }
        if (!CollectionUtils.isEmpty(noticeList)) {
            model.addAttribute("noticeList", noticeList);
        }

        if (tagMap != null && !tagMap.keySet().isEmpty()) {
            model.addAttribute("tagMap", tagMap);
        }

    }
}
